<?php
// +----------------------------------------------------------------------
// | QSCMS
// +----------------------------------------------------------------------
// | Copyright (c)  2025 https://www.qqss.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed QSCMS is not free software, commercial use must purchase official license from official website to avoid unnecessary legal disputes.
// +----------------------------------------------------------------------
// | Author: Contract 990504246@qq.com
// +----------------------------------------------------------------------

namespace app\adminapi\controller\plugin;

/**
 * 后台插件管理视图控制器
 */
class View extends Base
{
    /**
     * 插件管理首页
     */
    public function index()
    {
        return $this->render('admin/plugin/index', [
            'title'        => '插件管理',
            'current_page' => 'list'
        ]);
    }

    /**
     * 插件市场
     */
    public function market()
    {
        return $this->render('admin/plugin/market', [
            'title'        => '插件市场',
            'current_page' => 'market'
        ]);
    }

    /**
     * 插件配置
     */
    public function config()
    {
        $plugin_name = $this->request->get('plugin_name');

        if (empty($plugin_name)) {
            return $this->error('插件名称不能为空');
        }

        return $this->render('admin/plugin/config', [
            'title'        => '插件配置',
            'current_page' => 'config',
            'plugin_name'  => $plugin_name
        ]);
    }

    /**
     * 钩子管理
     */
    public function hooks()
    {
        return $this->render('admin/plugin/hooks', [
            'title'        => '钩子管理',
            'current_page' => 'hooks'
        ]);
    }

    /**
     * 获取插件配置HTML内容
     */
    public function getConfigHtml()
    {
        $plugin_name = $this->request->get('plugin_name');

        if (empty($plugin_name)) {
            return response('<div class="error-message">插件名称不能为空</div>');
        }

        try {
            $pluginManager   = new \app\common\plugin\PluginManager();
            $plugin_instance = $pluginManager->getPluginInstance($plugin_name);

            if (!$plugin_instance) {
                return response('<div class="error-message">插件不存在或未正确安装</div>');
            }

            // 获取插件配置
            $plugin_config = [];
            if (method_exists($plugin_instance, 'getAllConfig')) {
                $plugin_config = $plugin_instance->getAllConfig();
            } else {
                $plugin_config = $pluginManager->getPluginConfig($plugin_name);
            }

            // 获取配置字段定义
            $config_fields = [];
            if (method_exists($plugin_instance, 'getConfigFields')) {
                $config_fields = $plugin_instance->getConfigFields();
            }

            if (empty($config_fields)) {
                return response('<div class="info-message">该插件暂无可配置项</div>');
            }

            return $this->render('admin/plugin/config_form', [
                'plugin_name'   => $plugin_name,
                'plugin_config' => $plugin_config,
                'config_fields' => $config_fields
            ]);

        } catch (\Exception $e) {
            return response('<div class="error-message">加载插件配置失败: ' . $e->getMessage() . '</div>');
        }
    }
}
